﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace GaleSong.UI
{
    public class ResourceImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string image = parameter as string ?? value as string;
            if (image == null)
                return Binding.DoNothing;

            try
            {
                return ResourceImageHelper.GetResourceImage(image);
            }
            catch
            {
                return Binding.DoNothing;
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class BillionStringToShortStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is string))
                return null;
            string billionFormatString = "0,,.0M";
            string thousandsFormatString = "0,.0K";

            decimal dec;
            bool parsed = decimal.TryParse((string)value, out dec);
            if (parsed)
            {
                if (dec == 0)
                    return "0";
                else if (dec >= 0.1M * 1000000M)
                    return dec.ToString(billionFormatString);
                else
                    return dec.ToString(thousandsFormatString);
            }
            else
                return null;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public static class ResourceImageHelper
    {
        public static BitmapImage GetResourceImage(string image)
        {
            BitmapImage res = new BitmapImage();
            res.BeginInit();
            res.StreamSource = ModelAssemblyHelper.GetResourceStream(image);
            res.EndInit();
            res.Freeze();
            return res;
        }
    }

    public static class ModelAssemblyHelper
    {
        static Assembly modelAssembly = null;
        public static Assembly ModelAssembly
        {
            get
            {
                if (modelAssembly == null)
                    modelAssembly = typeof(ModelAssemblyHelper).Assembly;
                return modelAssembly;
            }
        }
        static string[] resourceNames = null;
        public static string GetResourcePath(string resourceName)
        {
            if (resourceNames == null)
                resourceNames = ModelAssembly.GetManifestResourceNames();
            foreach (string name in resourceNames)
                if (name.EndsWith(resourceName))
                    return name;
            return null;
        }
        public static Stream GetResourceStream(string resourceName)
        {
            return ModelAssembly.GetManifestResourceStream(GetResourcePath(resourceName));
        }
    }
}
